// 设备类型
<template>
  <div class="item1">
    <div class="contentTop">
      <div class="contentleft">
        <div class="title"> {{ $t("basic.equipment_type") }} </div>
        <div :gutter="24" class="searchBox">
          <el-form ref="form" :model="formData" label-width="0px" size="mini">
            <el-row :gutter="28">
              <el-col :xs="23" :sm="23" :md="23" :lg="23" :xl="23" style="padding-top:10px">
                <el-form-item label-position="left">
                  <el-input v-model="formData.keyword" clearable placeholder="" class="search">
                    <template slot="append">
                      <el-button type="primary" size="small" @click="handleSearch">{{ $t("common.query") }}</el-button>
                    </template>
                  </el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
          <div class="btngroup">
            <el-button type="primary" size="mini" @click="handleAdd()">{{ $t("common.add") }}</el-button>
            <el-button type="primary" size="mini" @click="handleEdit()">{{ $t("common.edit") }}</el-button>
            <el-button type="primary" size="mini" @click="handleDel()">{{ $t("common.remove") }}</el-button>
          </div>
        </div>

        <!-- 表格区域 -->
        <div class="tableAuto">
          <el-table :data="tableData" style="width:100%" :height="height" @row-click="rowClick" stripe
            :header-cell-style="{background:'#EAEBEC'}" v-loading="tableLoading" v-if="!isCard" ref="refTable" highlight-current-row
            @sort-change='sortChange'>
            <el-table-column label="" width="50" align="center">
              <template slot-scope="scope">
                <div>
                  <el-radio :label="scope.row.fid" v-model="radioId">&nbsp;</el-radio>
                </div>
              </template>
            </el-table-column>
            <!-- <el-table-column type="index" label="序号" width="50" align="center">
                            <template slot-scope="scope">
                                {{table_index(scope.$index+1)}}
                            </template>
                        </el-table-column> -->
            <!-- <el-table-column prop="fid" show-overflow-tooltip label="设备类型ID"></el-table-column> -->
            <el-table-column prop="className" width="130" show-overflow-tooltip :label="$t('basic.type_name')"
              sortable='custom'></el-table-column>
            <!-- <el-table-column prop="description" show-overflow-tooltip label="描述"></el-table-column> -->
          </el-table>
        </div>
        <div class="pagination-area">
          <el-pagination @current-change="handleCurrentChange" :page-size="pager.pageSize" :current-page="pager.pageNum"
            layout="prev, pager, next" :total="pager.total">
          </el-pagination>
          <!-- <el-pagination
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :page-sizes="[20, 30, 50, 100]"
                        :page-size="pager.pageSize"
                        :current-page="pager.pageNum"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="pager.total"
                    >
                    </el-pagination> -->
        </div>
      </div>
      <div class="contentRight">
        <!-- 阶段列表 -->
        <div class="title">{{ $t("basic.stage_list") }}</div>
        <form1 :parentFid="radioId"></form1>
      </div>
    </div>

    <!-- 单元参数 -->
    <div class="title" @click="unitCollapse = !unitCollapse">
      {{ $t("basic.unit_attr") }}
      <i class="el-icon-caret-bottom zhedie" v-show="unitCollapse"></i>
      <i class="el-icon-caret-top zhedie" v-show="!unitCollapse"></i>
    </div>
    <form2 v-show="unitCollapse" :parentFid="radioId" :equipmentClassList="equipmentClassList"></form2>

    <el-dialog v-dialogDrag :title="title" :visible.sync="isShowDialog" :close-on-click-modal="false"
      :width="screenWidth > 1024 ? '30%' : '50%'" class="isShowDialog" :before-close="handleClose">
      <el-form :model="ruleForm" label-width="95px" ref="ruleForm" size="small" :rules="rules" :hide-required-asterisk="true">
        <el-row :gutter="35">
          <el-col :xs="24">
            <el-form-item :label="$t('basic.type_name')" prop="className">
              <el-input v-model="ruleForm.className" placeholder="" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" offset="2">
            <el-form-item label="" label-width="50">
              <el-checkbox v-model="ruleForm.canStore" placeholder="" clearable>{{$t('basic.canStore')}}</el-checkbox>
              <el-checkbox v-model="ruleForm.canShip" placeholder="" clearable v-if="false">{{$t('basic.canShip')}}</el-checkbox>
              <el-checkbox v-model="ruleForm.canReceive" placeholder="" clearable v-if="false">{{$t('basic.canReceive')}}</el-checkbox>
            </el-form-item>

          </el-col>

          <el-col :xs="24">
            <el-form-item :label="$t('common.describe')" prop="description">
              <el-input type="textarea" v-model="ruleForm.description" placeholder="" clearable></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" size="small" @click="handleCreateMaterial('ruleForm')"
          :loading="loading">{{ $t("common.preservation") }}</el-button>
        <el-button @click="handleClose()" size="small">{{ $t("common.cancel") }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { configure } from "@/api/configure"
import form1 from "./template/form1.vue"
import form2 from "./template/form2.vue"
export default {
  components: {
    form1,
    form2
  },
  data () {
    return {
      screenWidth: document.body.clientWidth,
      height: 32 * 12.5,
      formData: {
        keyword: "",
      },
      ruleForm: {
        className: "",
        canStore: false,
        canShip: false,
        canReceive: false,
        description: "",
      },
      rules: {
        className: [{ required: true, message: '类型名称不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '描述不能为空', trigger: 'blur' }],
      },
      isShowDialog: false,
      icon: "icon-biaoge",
      title: this.$t('dialog.equip_cls_add'),
      loading: false,
      tableData: [],
      tableLoading: false,
      radioId: "",
      pager: {
        pageNum: 1,
        pageSize: 20,
        total: 0,
        field: '',
        order: ''
      },
      isCard: false,
      cardIndex: -1,
      icon: "icon-biaoge",
      currObj: {},
      isShow: true,
      phaseCollapse: false,
      unitCollapse: true,
      equipmentClassList: []
    }
  },
  created () {
    this.getEquipmentClassList()
  },
  watch: {
    screenWidth (val) {
      this.screenWidth = Number(val)
    },
    parentFid (val) {
      this.getEqpClassPhaseLinkList()
    }
  },
  mounted () {
    const that = this
    window.addEventListener('resize', function () {
      that.screenWidth = document.body.offsetWidth;
    })
  },
  methods: {
    handleSearch () {
      this.pager.pageNum = 1;
      this.getEquipmentClassList()
    },
    getEquipmentClassList () {
      const param = {
        className: this.formData.keyword,
        page: this.pager.pageNum,
        size: this.pager.pageSize,
        field: this.pager.field,
        order: this.pager.order,
      }

      this.tableLoading = true
      configure.getEquipmentClassList(param).then(res => {
        this.tableLoading = false
        if (res.code == 200) {
          this.tableData = res.data.rows
          this.pager.total = res.data.total

          this.tableData.forEach(item => {
            this.equipmentClassList.push({ label: item.className, value: item.fid })
          })
        }
      })
    },
    sortChange (column) {
      if (column.order === 'descending') {
        this.pager.field = column.prop
        this.pager.order = 'DESC'
      } else {
        this.pager.field = column.prop
        this.pager.order = 'ASC'
      }
      this.pager.pageNum = 1
      this.getEquipmentClassList()
    },
    handleSizeChange (val) {
      this.pager.pageNum = 1
      this.pager.pageSize = val
      this.getEquipmentClassList()
    },
    handleCurrentChange (val) {
      this.pager.pageNum = val;
      this.getEquipmentClassList()
    },
    handleClose () {
      this.isShowDialog = false
    },
    handleAdd () {
      this.title = this.$t('dialog.equip_cls_add')
      this.ruleForm = {
        className: "",
        canStore: false,
        canShip: false,
        canReceive: false,
        description: "",
      }
      this.isShowDialog = true
    },
    handleEdit () {
      if (this.radioId === '') {
        this.$message.error(this.$t('dialog.please_sel_edit'));
        return
      }
      this.title = this.$t('dialog.equip_cls_edit')
      let copyRow = this.tableData.find(item => item.fid == this.radioId)
      this.ruleForm = {
        fid: copyRow.fid,
        className: copyRow.className,
        canStore: copyRow.canStore == 1 ? true : false,
        canShip: copyRow.canShip == 1 ? true : false,
        canReceive: copyRow.canReceive == 1 ? true : false,
        description: copyRow.description,
      }
      this.isShowDialog = true
    },
    handleCreateMaterial (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const param = Object.assign({}, this.ruleForm)
          param.canStore = this.ruleForm.canStore ? 1 : 0
          param.canShip = this.ruleForm.canShip ? 1 : 0
          param.canReceive = this.ruleForm.canReceive ? 1 : 0
          this.loading = true
          configure.equipmentClassAdd(param, this.title).then(res => {
            this.loading = false
            if (res.code == 200) {
              this.$refs[formName].resetFields();
              this.getEquipmentClassList()
              this.isShowDialog = false
            }
            if (res.code == 202) {
              this.$message.error(res.msg);
            }
          }).catch(e => {
            this.loading = false
          })
        } else {
          return false
        }
      })
    },
    handleDel () {
      if (this.radioId === '') {
        this.$message.error(this.$t('dialog.please_sel_del'));
        return
      }
      let that = this
      this.$customConfirm({
        content: "请确认是否删除设备类型" + this.currObj.className,
        onok () {
          configure.removeEquipment(that.radioId).then(res1 => {
            if (res1.code == 200) {
              that.radioId = ""
              that.cardIndex = -1
              that.getEquipmentClassList()
              that.$message({ duration: 1000, type: 'success', message: '删除成功!' });
            } else {
              that.$message.error(res1.msg);
            }
          })
        }
      });
    },
    rowClick (row) {
      this.radioId = row.fid;
      this.currObj = row;
      this.$refs.refTable.setCurrentRow(row);
      // this.equipmentClassList = [{
      //   label: row.className,
      //   value: row.fid
      // }]
    },
    handleIndex (index, row) {
      this.cardIndex = index
      this.radioId = row.fid;
    },
    changePattern () {
      this.isCard = !this.isCard
      this.radioId = ''
      this.cardIndex = -1
      this.icon = this.isCard ? 'icon-qiehuan' : 'icon-biaoge'
    },
    table_index (index) {
      return (this.pager.pageNum - 1) * this.pager.pageSize + index
    },
  }
}
</script>

<style scoped>
.contentTop {
  display: flex;
}
.change >>> .iconfont {
  font-size: 13px !important;
}
.titmepicker >>> .el-date-editor {
  width: 100%;
}
.titmepicker >>> .el-date-editor .el-range-separator {
  width: 8%;
}
.isShowDialog >>> .el-select {
  width: 100%;
}
.tableAuto,
.cardBox {
  clear: both;
}
.pagination-area {
  padding: 5px;
  border: 1px solid #eaebec;
  border-top: none;
}
.Desc {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.cardBox {
  overflow-y: auto;
}
.card {
  background: #eaebec;
  margin-bottom: 20px;
  padding: 10px;
  border-radius: 3px;
}
.card p {
  font-size: 12px;
  line-height: 22px;
  transform: scale(0.9);
}
.desc {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.title {
  font-weight: bold;
}
.selectCard {
  background: #f6fbff;
}
@media only screen and (min-width: 1200px) {
  .el-col-lg-5 {
    width: 19.83333%;
  }
}
.title {
  line-height: 35px;
  background: #3b2e58;
  color: #fff;
  padding: 0 15px;
  font-size: 14px;
  margin-top: 10px;
  cursor: pointer;
}
.zhedie {
  float: right;
  line-height: 35px;
  font-size: 18px;
  cursor: pointer;
}
.contentTop .contentleft {
  width: 200px;
  border-right: 1px solid #e4e4e4;
  height: 100%;
}
.contentTop .contentRight {
  flex: 1;
  padding-left: 15px;
}
.search >>> .el-input-group__append,
.search >>> .el-input-group__prepend {
  background: #00a09d;
  color: #fff;
}
.tableAuto >>> .el-table__row {
  background: none !important;
}
.tableAuto >>> .el-table--striped .el-table__body tr.el-table__row--striped td {
  background: none;
}
.tableAuto >>> .el-table__row td {
  padding: 8px 0;
}
.tableAuto >>> .el-table thead {
  display: none;
}
.btngroup >>> .el-button--mini {
  padding: 7px 12px;
  font-size: 12px;
  /* transform:scale(0.8);
        margin: 0; */
}
.searchBox >>> .el-form-item--mini.el-form-item {
  margin-bottom: 4px;
}
</style>
